package jp.bsws.action.master;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import jp.bsws.bean.ShiharaiHohoBean;
import jp.bsws.common.BSWSConstants;
import jp.bsws.common.BSWSSession;
import jp.bsws.common.BSWSUtils;
import jp.bsws.common.DBUtils;
import jp.bsws.dao.BSWSDao;
import jp.bsws.dao.ShiharaiHohoDao;
import jp.bsws.model.ShiharaiHohoModel;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

/*
 * メインメニューアクションクラス
 */
public class ShiharaiHohoMasterAction extends ActionSupport {
	private static final long serialVersionUID = 1L;

	private BSWSSession bSWSSession;

	public String getLoginJugyoinKnj() { return bSWSSession.getStrJugyoinKnj(); }

	private List<ShiharaiHohoBean> shiharaiHohoBeanList;
	private Integer intTaisyoSeq;

	public void validateUpdateRaitenYoin() {

		//セッション情報を取得
		HttpSession session = ServletActionContext.getRequest().getSession();
		bSWSSession = (BSWSSession) session.getAttribute(BSWSConstants.KEY_SESSION);

		//必須チェック
//		for(int i =0; i < raitenYoinBeanList.size(); i++){
//			RaitenYoinBean raitenYoinBean = raitenYoinBeanList.get(i);
//			if(intTaisyoSeq == raitenYoinBean.getSeq() && raitenYoinBean.isChkEdit()){
//				if (BSWSUtils.isBlankNull(raitenYoinBean.getStrRaitenYoinKnj())) {
//					addFieldError("raitenYoinBeanList[" + i + "].strRaitenYoinKnj", (i + 1) + "行目の来店要因名称を入力して下さい。");
//				}
//				break;
//			}
//		}
	}

	private void init() throws Exception{
		//セッション情報を取得
		HttpSession session = ServletActionContext.getRequest().getSession();
		bSWSSession = (BSWSSession) session.getAttribute(BSWSConstants.KEY_SESSION);
	}

	@Override
	public String execute() throws Exception{

		//初期設定
		init();

		Connection con = DBUtils.getConnection();
		try{
			//画面表示内容の設定
			hyojiJunbi(con);
		}finally{
			con.close();
		}

		return SUCCESS;
	}

	/**
	 * 来店要因追加
	 * @return SUCCESS
	 * @throws Exception
	 */
	public String add() throws Exception{

		init();

		for(ShiharaiHohoBean shiharaiHohoBean : this.shiharaiHohoBeanList) {
			shiharaiHohoBean.setStrGroupCod(bSWSSession.getStrGroupCod());
			shiharaiHohoBean.setHontaiKeijoList(BSWSConstants.hontaiKeijoList);
			shiharaiHohoBean.setHyojiKbnList(BSWSConstants.hyojiKbnList);
		}

		//支払方法行追加
		ShiharaiHohoBean shiharaiHohoBean = new ShiharaiHohoBean();
		shiharaiHohoBean.setStrGroupCod(bSWSSession.getStrGroupCod());
		shiharaiHohoBean.setHontaiKeijoList(BSWSConstants.hontaiKeijoList);
		shiharaiHohoBean.setHyojiKbnList(BSWSConstants.hyojiKbnList);
		this.shiharaiHohoBeanList.add(shiharaiHohoBean);

		return SUCCESS;
	}

	public String updateShiharaiHoho() throws Exception{

		ShiharaiHohoDao shiharaiHohoDao = ShiharaiHohoDao.getInstance();

		//初期設定
		init();

		Connection con = DBUtils.getConnection();
		try{

			//支払方法情報更新対象取得
			for(ShiharaiHohoBean shiharaiHohoBean : shiharaiHohoBeanList) {
				int cnt = 0;
				if(shiharaiHohoBean.getSeq() != null){
					//情報更新
					cnt = shiharaiHohoDao.updateShiharaiHoho(con, bSWSSession, shiharaiHohoBean);
				}else{
					if(BSWSUtils.isNotBlankNull(shiharaiHohoBean.getStrShiharaiHohoKnj())
							&& BSWSUtils.isNotBlankNull(shiharaiHohoBean.getStrShiharaiHohoRyakuKnj())
							&& BSWSUtils.isNotBlankNull(shiharaiHohoBean.getStrShiharaiHohoHyojiJun())
							&& BSWSUtils.isNotBlankNull(shiharaiHohoBean.getStrShiharaiHohoHyojiFlg())
							&& BSWSUtils.isNotBlankNull(shiharaiHohoBean.getStrShiharaiHohoHontaiKeijoFlg())
					){
						//情報追加
						cnt = shiharaiHohoDao.insertShiharaiHoho(con, null, bSWSSession, shiharaiHohoBean);
					}
				}
				if(cnt == 0){
					//処理なし
				}
			}

			//画面表示内容の設定
			hyojiJunbi(con);

			con.commit();
		}catch(SQLException se){
			con.rollback();
			throw se;
		}finally{
			con.close();
		}

		return SUCCESS;
	}

	public String deleteShiharaiHoho() throws Exception{

		BSWSDao bSWSDao = BSWSDao.getInstance();

		//初期設定
		init();

		Connection con = DBUtils.getConnection();
		try{
			//支払方法削除
			int cnt = bSWSDao.deleteRow(con, intTaisyoSeq, " M_SHIHARAI_HOHO ");
			if(cnt == 0){
				//処理なし
			}

			//画面表示内容の設定
			hyojiJunbi(con);

			con.commit();
		}catch(SQLException se){
			con.rollback();
			throw se;
		}finally{
			con.close();
		}

		return SUCCESS;
	}

	private void hyojiJunbi(Connection con) throws Exception{

		//支払方法マスタ情報を取得
		getShiharaiHohoJoho(con);

		//支払方法行追加
		ShiharaiHohoBean shiharaiHohoBean = new ShiharaiHohoBean();
		shiharaiHohoBean.setStrGroupCod(bSWSSession.getStrGroupCod());
		shiharaiHohoBean.setHontaiKeijoList(BSWSConstants.hontaiKeijoList);
		shiharaiHohoBean.setStrShiharaiHohoHontaiKeijoFlg(BSWSConstants.C_HONTAI_KEIJO);
		shiharaiHohoBean.setHyojiKbnList(BSWSConstants.hyojiKbnList);
		shiharaiHohoBeanList.add(shiharaiHohoBean);
	}

	private void getShiharaiHohoJoho(Connection con) throws Exception{

		ShiharaiHohoDao shiharaiHohoDao = ShiharaiHohoDao.getInstance();
		this.shiharaiHohoBeanList = new ArrayList<ShiharaiHohoBean>();
		this.shiharaiHohoBeanList.clear();

		//支払方法マスタ情報を取得
		List<ShiharaiHohoModel> shiharaiHohoList = shiharaiHohoDao.getShiharaiHohoJoho(con, null, bSWSSession.getStrGroupCod(), null);
		for(int i = 0; i < shiharaiHohoList.size(); i++){
			ShiharaiHohoBean shiharaiHohoBean = new ShiharaiHohoBean();
			shiharaiHohoBean.setSeq(shiharaiHohoList.get(i).getSeq());
			shiharaiHohoBean.setStrGroupCod(bSWSSession.getStrGroupCod());
			shiharaiHohoBean.setStrShiharaiHohoKnj(shiharaiHohoList.get(i).getStrShiharaiHohoKnj());
			shiharaiHohoBean.setStrShiharaiHohoRyakuKnj(shiharaiHohoList.get(i).getStrShiharaiHohoRyakuKnj());
			shiharaiHohoBean.setStrShiharaiHohoHyojiJun(String.valueOf(shiharaiHohoList.get(i).getIntShiharaiHohoHyojiJun()));
			shiharaiHohoBean.setStrShiharaiHohoHyojiFlg(shiharaiHohoList.get(i).getStrShiharaiHohoHyojiFlg());
			shiharaiHohoBean.setStrShiharaiHohoHontaiKeijoFlg(shiharaiHohoList.get(i).getStrShiharaiHohoHontaiKeijoFlg());
			shiharaiHohoBean.setTenpoList(bSWSSession.getSansyoTenpoList());
			shiharaiHohoBean.setHontaiKeijoList(BSWSConstants.hontaiKeijoList);
			shiharaiHohoBean.setHyojiKbnList(BSWSConstants.hyojiKbnList);
			this.shiharaiHohoBeanList.add(shiharaiHohoBean);
		}
	}

	public BSWSSession getbSWSSession() {
		return bSWSSession;
	}

	public void setbSWSSession(BSWSSession bSWSSession) {
		this.bSWSSession = bSWSSession;
	}

	public List<ShiharaiHohoBean> getShiharaiHohoBeanList() {
		return shiharaiHohoBeanList;
	}

	public void setShiharaiHohoBeanList(List<ShiharaiHohoBean> shiharaiHohoBeanList) {
		this.shiharaiHohoBeanList = shiharaiHohoBeanList;
	}

	public Integer getIntTaisyoSeq() {
		return intTaisyoSeq;
	}

	public void setIntTaisyoSeq(Integer intTaisyoSeq) {
		this.intTaisyoSeq = intTaisyoSeq;
	}

}